package com.codyy.lib_common.base.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/**
 * Fragment懒加载(DataBinding + ViewModel)
 */
abstract class BaseDataBindingFragment<DB : ViewDataBinding> : BaseFragment {
    constructor() : super()
    constructor(@LayoutRes contentLayoutId: Int) : super(contentLayoutId)
    lateinit var mDataBinding: DB

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (mRootView == null) {
            mDataBinding = DataBindingUtil.inflate(inflater, getLayoutRes(), container, false)
            // 让xml内绑定的LiveData和Observer建立连接，让LiveData能感知Activity的生命周期
            mDataBinding.lifecycleOwner = this
            mRootView = mDataBinding.root
        }
        return mRootView
    }

    override fun onDestroy() {
        super.onDestroy()
        mDataBinding.unbind()
    }
}